# -*- coding: utf-8 -*-

from odoo import models, fields, api, _
from odoo.exceptions import UserError


class SForecastMaterialLine(models.Model):
    _name = 's.forecast.material.line'
    _description = 'Forecast Material Line'

    # Field definitions
    forecast_order_line_id = fields.Many2one(
        's.forecast.order.line',  # Updated to s.forecast.order.line
        string='Forecast Order Line',  # Forecast order line
        required=True,
        ondelete='cascade',  # Cascade delete when the main record is deleted
    )
    material_id = fields.Many2one(
        'product.product',
        string='Material',  # Material
    )
    required_quantity = fields.Float(
        string='Required Quantity',  # Required quantity
        required=True,
        default=0.0
    )
    source = fields.Selection([
        ('bom', 'From BOM'),  # From BOM
        ('manual', 'Manual Added'),  # Manually added
    ], string='Source', default='manual', required=True)  # Material source

    # On Hand Quantity (real-time display)
    on_hand_quantity = fields.Float(
        string='On Hand Quantity',  # On Hand Quantity
        compute='_compute_quantities',  # Computed field
        store=True,  # Store the computed result
        help="Current on-hand quantity of this material."  # Current on-hand quantity of this material.
    )
    # Suggested Purchase Quantity
    suggested_purchase_quantity = fields.Float(
        string='Suggested Purchase Quantity',  # Suggested Purchase Quantity
        compute='_compute_quantities',  # Computed field
        store=True,  # Store the computed result
        help="Suggested purchase quantity = Required Quantity - On Hand Quantity (zero if the result is less than zero)."
        # Suggested purchase quantity = Required Quantity - On Hand Quantity (zero if the result is less than zero).
    )
    base_bom_qty = fields.Float(
        string='Base BOM Quantity',
        help="Base BOM quantity for this material line.",
        digits=(16, 5),
        default=1.0
    )
    uom_id = fields.Many2one('uom.uom', string='Unit of Measure', related='material_id.uom_id', help="Unit of measure for the material.")  # Unit of measure for the material

    # Compute on-hand quantity and suggested purchase quantity
    @api.depends('material_id', 'required_quantity')
    def _compute_quantities(self):
        for rec in self:
            if rec.material_id:
                # Get the on-hand quantity of the material
                rec.on_hand_quantity = rec.material_id.qty_available
                # Calculate suggested purchase quantity
                rec.suggested_purchase_quantity = max(0.0, rec.required_quantity - rec.on_hand_quantity)
            else:
                rec.on_hand_quantity = 0.0
                rec.suggested_purchase_quantity = 0.0

    @api.onchange('base_bom_qty')
    def onchange_base_bom_qty(self):
        for rec in self:
            if rec.base_bom_qty <= 0:
                raise UserError(_("Base BOM Quantity must be greater than zero."))
            else:
                rec.required_quantity = rec.base_bom_qty * rec.forecast_order_line_id.market_forecast_quantity
                # # Update suggested purchase quantity based on the new base BOM quantity
                # rec.suggested_purchase_quantity = max(0.0, rec.required_quantity - rec.on_hand_quantity)
